/**
 * Created by CJB on 2017-02-17 10:12:41.
 */

(function(){
	$(function () {
	    initUpdateWindow().dialog("open");
	});
	
	/**
	 * 初始化更新窗口
	 * @returns {Function}
	 */
	function initUpdateWindow(){
	
	    //声明消息提示
	    var toolTips = new Object();
	
	    //设置表单验证
	    var validator = $("#updateForm").validate({
	        rules:{
	        	userId:{
		            "required":true,
		            digits:true,
	        	},
	            id:"required"
	        },
	        messages:{
	            userId:{
		            "required":"请输入修改人的id",
		            digits:"只能输入数字",
	        	},
	            id:"不能为空"
	        },
	        errorPlacement: function(eror,element){
	            var toolTip = createTooltip(toolTips,element.attr("id"));
	            toolTip.setContent(eror.text());
	            element.addClass("error-border");
	        },
	        success:function(label){
	            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
	            var toolTip = toolTips[elementId];
	            $("#" + elementId).removeClass("error-border");
	            toolTip.close();
	            toolTip.detach();
	        },
	        submitHandler:function(form){
	        	post(getPath() + "app/log/edit/" + $("#id").val(),
	                $("#updateForm").serialize(),
	                function(resultObject){
		                var errors = resultObject.errors;
		                //判断后台是否有返回错误消息
		                if(errors && errors.length > 0){
		                    for(var i=0; i < errors.length; i++){
		                        var error = errors[i];
		                        var toolTip = createTooltip(toolTips,"update" + error.field);
		                        toolTip.setContent(error.defaultMessage);
		                        $("#update" + error.field).addClass("error-border");
		                    }
		                }else{
		                    //更新成功，弹出添加成功提示框，当用户点击确定后刷新页面
		                    updateWindow.dialog("close");
		                    createWindow("更新成功",{url:getPath() + "app/log/list/" + $("#pagination").jqPaginator('option',"currentPage") + "/" + pageSize}).dialog("open");
		                }
	    		});
	        }
	    });
	    //初始化添加窗口
	    var updateWindow = createEditWindow("updateDialog","更新","updateForm",{width:400,height:85});
	
	    return updateWindow;
	}
})();